package com.example.ljw.frescowallpaper.view;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.ljw.frescowallpaper.R;
import com.example.ljw.frescowallpaper.base.BaseActivity;



import com.example.ljw.frescowallpaper.bean.UserBean;
import com.example.ljw.frescowallpaper.config.Constants;
import com.example.ljw.frescowallpaper.utils.PreUtils;

import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;


public class LoginActivity extends BaseActivity implements View.OnClickListener{

    private Context mContext = this;
    private TextView registerTextView;
    private Button login_btn;
    private EditText userId_loginEdt,userPsw_loginEdt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        initView();
        

    }

    private void login() {
        String id = userId_loginEdt.getEditableText().toString();
        String password = userPsw_loginEdt.getEditableText().toString();
        if(id.equals("")||password.equals("")){
            Toast.makeText(this, "帐号或密码不能为空", Toast.LENGTH_LONG).show();
            return;
        }
        BmobQuery<UserBean> query = new BmobQuery<>();
        query.addWhereEqualTo("userID",id);
        query.addWhereEqualTo("userPsw",password);
        query.findObjects(new FindListener<UserBean>() {
            @Override
            public void done(List<UserBean> list, BmobException e) {
                if (e==null){
                    String objectId = list.get(0).getObjectId();
                    String gid = list.get(0).getUserID();
                    String gpsw = list.get(0).getUserPsw();
                    String gname = list.get(0).getUserName();
                    String gicon = list.get(0).getIconUrl();
                    String id = userId_loginEdt.getEditableText().toString();
                    String password = userPsw_loginEdt.getEditableText().toString();
                    if (gid.equals(id)&&gpsw.equals(password)){
                        Toast.makeText(LoginActivity.this, gname+"登录成功", Toast.LENGTH_SHORT).show();
                        //存入用户名
                        PreUtils.writeString(LoginActivity.this,Constants.KEY_HAVA_USER,gname);
                        //存入头像地址
                        PreUtils.writeString(LoginActivity.this,Constants.KEY_WITH_ICON,gicon);
                        //存入objectid
                        PreUtils.writeString(LoginActivity.this, Constants.KEY_OF_OBJECTID,objectId);
                        Intent intent = new Intent();
                        intent.putExtra("username",gname);
                        intent.putExtra("usericon",gicon);
                        setResult(Constants.ASK_FOR_USER_RESULT,intent);
                        finish();
                    }
                }else {
                    Toast.makeText(LoginActivity.this, "账号或密码有错", Toast.LENGTH_SHORT).show();
                }
            }
        });


    }

    private void initView() {
        registerTextView = (TextView) findViewById(R.id.register_textview_login);
        registerTextView.setOnClickListener(this);
        userId_loginEdt = (EditText) findViewById(R.id.userid_login);
        userPsw_loginEdt = (EditText) findViewById(R.id.userpsw_login);
        login_btn = (Button) findViewById(R.id.login_btn);
        login_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                login();
            }
        });
    }

    @Override
    public void onClick(View view) {
        Intent intent = new Intent();
        switch (view.getId()){
            case R.id.register_textview_login:
                intent.setClass(mContext,RegisterActivity.class);
                break;

        }
        startActivity(intent);
    }
}
